package com.cmfi.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cmfi.dao.PlcDao.PlcDao;
import com.cmfi.dao.PlcDao.PlcNodeDao;
import com.cmfi.domain.PlcEntity.Plc;
import com.cmfi.domain.PlcEntity.PlcNode;
import com.cmfi.service.inter.PlcInter.PlcNodeService;

@Service
public class PlcNodeServiceImpl implements PlcNodeService {

	@Autowired
	private PlcNodeDao plcNodeDao;

	@Autowired
	private PlcDao plcDao;
	
	static Logger logger = LoggerFactory.getLogger(PlcNodeServiceImpl.class);

	@Override
	public List<PlcNode> getAllPlcNode() {
		return plcNodeDao.getAllPlcNode();
	}
	
	@Override
	public void addPlcNode(PlcNode node) {
		plcNodeDao.savePlcNode(node);
	}

	@Override
	public void delPlcNode(Long id) {
		plcNodeDao.deleteById(id);
	}

	@Override
	public List<PlcNode> getAllPlcNodeNeedSubscribe() {
		List<Plc> plcs = plcDao.getAllPlc();
		Map<Long, Plc> plcMap = new HashMap<>();
		for (Plc plc : plcs) {
			plcMap.put(plc.getPlcNum(), plc);
		}
		List<PlcNode> plcNodes = plcNodeDao.getAllPlcNodeNeedSubscribe(true);
		for (PlcNode node : plcNodes) {
			Plc plc = plcMap.get(node.getPlcNum());
			if (plc == null) {
				continue;
			}
			node.setPlcConnectAddress(plc.getConnectAddress());
		}
		return plcNodes;
	}

}
